<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>Title</title>
    <script src="https://cdn.bootcdn.net/ajax/libs/jquery/3.6.0/jquery.min.js"></script>
    <link href="https://cdn.bootcdn.net/ajax/libs/twitter-bootstrap/3.4.1/css/bootstrap.min.css" rel="stylesheet">
    <script src="https://cdn.bootcdn.net/ajax/libs/twitter-bootstrap/3.4.1/js/bootstrap.min.js"></script>
</head>
<body>
<div class="container-fluid" style="margin-top: 40px">
    <div class="row">
        <div class="col-md-4 col-md-offset-4">
            <h2 class="text-center">注册页面</h2>
            <form id="regform" novalidate>
                {% csrf_token %}
                {% for form in form_obj %}
                    <div class="form-group">
                        <label for="{{ form.auto_id }}">{{ form.label }}:</label>
                        {{ form }}
                        <span style="color: red" class="pull-right"></span>
                    </div>
                {% endfor %}
                <div class="form-group">
                    <label for="my_avatar">头像:
                        {% load static %}
                        <img id="my_img" src="{% static 'img/default.png' %}" alt="" width="100" height="100"
                             style="margin-left: 10px">
                    </label>
                    <input type="file" id="my_avatar" style="display: none">
                </div>
                <input type="button" id="id_commit" class="button btn-primary pull-right" value="提交">
            </form>
            <br>
            <hr>
            <br>
            <a href="/user/login/" class="pull-right">已有账号，登录>></a>
        </div>
    </div>
</div>


<script>
    //实时展示头像
    $('#my_avatar').change(function () {
        //文件阅读器
        let my_AvatarReadObj = new FileReader();
        //获取用户上传头像
        let fileobj = $(this)[0].files[0];
        //将文件对象交给文件阅读器
        my_AvatarReadObj.readAsDataURL(fileobj)
        //展示
        my_AvatarReadObj.onload = function () {
            $('#my_img').attr('src', my_AvatarReadObj.result)
        }
    })

    //向后端提交数据
    $('#id_commit').click(function () {
        let formDataObj = new FormData;
        //循环数组，拿到普通键值对数据
        $.each($('#regform').serializeArray(), function (index, obj) {
            formDataObj.append(obj.name, obj.value)
        })

        //添加文件数据
        formDataObj.append('avatar', $('#my_avatar')[0].files[0]);

        //发送ajax请求
        $.ajax({
            url: '',
            type: 'post',
            data: formDataObj,

            contentType: false,  //不需要任何编码
            processData: false,  //前端浏览器不需要做任何处理

            success: function (args) {

                if (args.code === 1000) {
                    //注册成功，则跳转到登录页面
                   window.location.href = args.url

                } else {
                    //注册失败，则渲染错误信息
                    $.each(args.msg, function (name, obj) {
                        let targerId = '#id_' + name
                        $(targerId).next().text(obj[0]).parent().addClass('has-error')
                    })

                }}
            })
    })

    //给所有input标签绑定焦点事件，需要清除渲染的错误信息，和消除has-error
    $('input').focus(function (){
        $(this).next().text('').parent().removeClass('has-error')
    })

</script>
</body>
</html>